/**
 * 
 */
package com.antilia.infomaresme.form;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.IFormSubmittingComponent;

/**
 * @author Ernesto Reinaldo Barreiro
 *
 */
public class ErrorHighlightBehavior extends Behavior {
	
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor.
	 */
	public ErrorHighlightBehavior() {
	}

	public void onComponentTag(Component component, ComponentTag tag)
	{
		FormComponent<?> fc = (FormComponent<?>)component;
		if (!fc.isValid() && !(fc instanceof IFormSubmittingComponent)) {
			String classes = tag.getAttribute("class");
			tag.put("class", classes != null? (classes + " hasError"): "hasError");			
		} else {
			String classes = tag.getAttribute("class");
			tag.put("class", classes != null? (classes + " "): "");
		}
	}
	
}
